package com.seek.eduorder.controller;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.seek.commonutils.ResponseResult;
import com.seek.eduorder.config.AlipayConfig;
import com.seek.eduorder.entity.Order;
import com.seek.eduorder.service.PayLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author wml
 * @since 2022-03-02
 */
@RestController
@RequestMapping("/eduorder/paylog")
public class PayLogController {

    @Autowired
    private PayLogService payLogService;

    // 生成微信支付二维码接口，参数是订单号
    @GetMapping("createNative/{orderNo}")
    public ResponseResult createNative(@PathVariable String orderNo) {
        // 返回信息，包含二维码地址，还有其他需要的信息
        Map map = payLogService.createNative(orderNo);
        System.out.println("****返回二维码 map 集合:"+map);
        return ResponseResult.ok().data(map);
    }

    // 查询订单支付状态
    // 参数：订单号，根据订单号查询支付状态
    @GetMapping("queryPayStatus/{orderNo}")
    public ResponseResult queryPayStatus(@PathVariable String orderNo) {
        Map<String, String> map = payLogService.queryPayStatus(orderNo);
        System.out.println("*****查询订单状态map集合:" + map);
        if(map == null) {
            return ResponseResult.error().message("支付出错了");
        }
        // 如果返回 map 里面不为空，通过 map 获取订单状态
        if(map.get("trade_state").equals("SUCCESS")) { // 支付成功
            // 添加记录到支付表，更新订单表订单状态
            payLogService.updateOrdersStatus(map);
            return ResponseResult.ok().message("支付成功");
        }
        return ResponseResult.ok().code(2500).message("支付中");
    }

    // 生成支付宝支付接口，参数是订单号
    @GetMapping("createAlipayNative")
    public ResponseResult createAlipayNative(Order order) {
        // 获得初始化的 AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);

        // 设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);

        // 商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = order.getOrderNo();
        // 付款金额，必填
        String total_amount = order.getTotalFee().toString();
        // 订单名称，必填
        String subject = order.getCourseTitle();
        // 商品描述，可空
        String body = "qwe";

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        //若想给BizContent增加其他可选请求参数，以增加自定义超时时间参数timeout_express来举例说明
        //alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
        //		+ "\"total_amount\":\""+ total_amount +"\","
        //		+ "\"subject\":\""+ subject +"\","
        //		+ "\"body\":\""+ body +"\","
        //		+ "\"timeout_express\":\"10m\","
        //		+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        //请求参数可查阅【电脑网站支付的API文档-alipay.trade.page.pay-请求参数】章节

        // 请求
        String result = null;
        try {
            result = alipayClient.pageExecute(alipayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }

        // 输出
//        System.out.println(result);
//        return ResponseResult.ok().code(2500).message("支付中");
        return ResponseResult.ok().data("result", result);
    }

}

